<template>
  <div class="xtx-checkbox" @click="changeChecked()">
    <i v-if="checked" class="iconfont icon-checked"></i>
    <i v-else class="iconfont icon-unchecked"></i>
    <span v-if="$slots.default"><slot /></span>
  </div>
</template>
<script>
// import { ref, watch } from 'vue'
import { useVModel } from '@vueuse/core'

export default {
  name: 'XtxCheckbox',
  props: {
    modelValue: {
      type: Boolean,
      default: false
    }
  },

  setup (props, context) {
    // const checked = ref(false)

    /* watch(() => props.modelValue, () => {
      checked.value = props.modelValue
    }, { immediate: true }) */
    const checked = useVModel(props, 'modelValue', context.emit)

    const changeChecked = () => {
      // 通知父组件
      const newValue = !checked.value
      checked.value = newValue

      // 让组件拥有change事件
      context.emit('change', newValue)
    }

    return {
      checked, changeChecked
    }
  }
}
</script>
<style scoped lang="less">
.xtx-checkbox {
  display: inline-block;
  margin-right: 2px;
  .icon-checked {
    color: @xtxColor;
    ~ span {
      color: @xtxColor;
    }
  }
  i {
    position: relative;
    top: 1px;
  }
  span {
    margin-left: 2px;
  }
}
</style>
